package com.intelligentpension.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import com.intelligentpension.utils.WifiMangerUtil

/**
 * 版权：点赞 版权所有
 * author :赵航
 * @e-mail: zhaohangcn@gmail.com
 * time：2022/4/9
 * describe ：
 */
class WifiBroadcast(var mListener: OnBroadListener) : BroadcastReceiver() {
    private var mBroadcastReceptionNumber: Int = 0 //每一次连接的信息都是两次
    private var mManager: WifiMangerUtil? = null

    interface OnBroadListener {
        fun onRefresh()
    }

    override fun onReceive(p0: Context?, p1: Intent?) {
        mManager = WifiMangerUtil(p0!!)
        if (p1 == null) return
        var state = p1.action
        when (state) {

            //更新wifi列表
            WifiManager.SCAN_RESULTS_AVAILABLE_ACTION -> {
                mListener.onRefresh()
            }
            //连接上wifi
            WifiManager.NETWORK_STATE_CHANGED_ACTION -> {
                //获取当前wifi的信息
                var info = p1.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO)
                //如果当前的wifi状态是连接状态
                if (info!!.state.equals(NetworkInfo.State.CONNECTED)) {
                    ++mBroadcastReceptionNumber
                    var wifiInfo = mManager!!.getWifiManger()!!.connectionInfo
                    if (mBroadcastReceptionNumber%2==0){
                        //取消网络配置
                        
                    }
                }
            }
            //密码错误
            WifiManager.SUPPLICANT_STATE_CHANGED_ACTION->{

            }
        }
    }
}